using System;
using CalcSharp.Core;
using dnAnalytics.Math;

namespace CalcSharp
{
    #region Complex math
    sealed class CPLXMath
    {
        private CPLXMath() { }
        public static Complex Add(string[] array)
        {
            Complex result = new Complex(0, 0);
            int i;
            for (i = 1; i < array.Length; i++)
            {
                if (array[i] == ">" || array[i] == "*>" || array[i] == "+>" || array[i] == "->" || array[i] == "/>") break;
                result += Program.loaded.CPLX_mem[array[i]];
            }
            return result;
        }
        public static Complex Sub(string[] array)
        {
            Complex result = new Complex(0, 0);
            int i;
            for (i = 1; i < array.Length; i++)
            {
                if (array[i] == ">" || array[i] == "*>" || array[i] == "+>" || array[i] == "->" || array[i] == "/>") break;
                result -= Program.loaded.CPLX_mem[array[i]];
            }
            return result;
        }
        public static Complex Mul(string[] array)
        {
            Complex result = new Complex(0, 0);
            int i;
            for (i = 1; i < array.Length; i++)
            {
                if (array[i] == ">" || array[i] == "*>" || array[i] == "+>" || array[i] == "->" || array[i] == "/>") break;
                result *= Program.loaded.CPLX_mem[array[i]];
            }
            return result;
        }
        public static Complex div(string[] array)
        {
            Complex result = new Complex(0, 0);
            int i;
            for (i = 1; i < array.Length; i++)
            {
                if (array[i] == ">" || array[i] == "*>" || array[i] == "+>" || array[i] == "->" || array[i] == "/>") break;
                result /= Program.loaded.CPLX_mem[array[i]];
            }
            return result;
        }
        public static Complex pow(Complex value1, Complex value2)
        {
            return ComplexMath.Pow(value1, value2);
        }
        public static Complex sqr(Complex value1, Complex value2)
        {
            return ComplexMath.Pow(value1, ComplexMath.Conjugate(value2));
        }
        public static Complex sin(Complex value)
        {
            return ComplexMath.Sin(value);
        }
        public static Complex cos(Complex value)
        {
            return ComplexMath.Cos(value);
        }
        public static Complex tan(Complex value)
        {
            return ComplexMath.Tan(value);
        }
        public static Complex ctg(Complex value)
        {
            return ComplexMath.Cos(value) / ComplexMath.Sin(value);
        }
        public static Complex arcsin(Complex value)
        {
            return ComplexMath.Asin(value);
        }
        public static Complex arccos(Complex value)
        {
            return ComplexMath.Acos(value);
        }
        public static Complex arctan(Complex value)
        {
            return ComplexMath.Atan(value);
        }
        public static double Abs(Complex value)
        {
            return ComplexMath.Absolute(value);
        }

        public static Complex ParseComplex(string input)
        {
            string[] buffer = input.Split(';');
            if (buffer.Length < 2) throw new CSException("Invalid Complex format");
            else return new Complex(Convert.ToDouble(buffer[0]), Convert.ToDouble(buffer[1]));
        }
    }
    #endregion
}